๊ตฌ์กฐ ๋ถํด ํ ๋น์ผ๋ก ์๋ฐ์คํฌ๋ฆฝํธ ํจํด ๋งค์นญ์ ๊ฐ๋ ฅํจ์ ๊ฒฝํํด ๋ณด์ธ์. ๋ ๊น๋ํ๊ณ ๊ฐ๋ ์ฑ ๋์ ์ฝ๋๋ฅผ ์ํ ๊ณ ๊ธ ๊ธฐ๋ฒ, ์ค์ ์์ , ๋ชจ๋ฒ ์ฌ๋ก๋ฅผ ์์๋ด ๋๋ค.
์๋ฐ์คํฌ๋ฆฝํธ ํจํด ๋งค์นญ: ๊ตฌ์กฐ ๋ถํด ํ ๋น ๋ง์คํฐํ๊ธฐ
๋์์์ด ๋ฐ์ ํ๋ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ๋ฐ ํ๊ฒฝ์์ ๊น๋ํ๊ณ , ๊ฐ๊ฒฐํ๋ฉฐ, ์ ์ง๋ณด์ํ๊ธฐ ์ฌ์ด ์ฝ๋๋ฅผ ์์ฑํ๋ ๊ฒ์ ๋งค์ฐ ์ค์ํฉ๋๋ค. ์ด ๋ชฉํ๋ฅผ ๋ฌ์ฑํ๋ ๋ฐ ๋์์ด ๋๋ ๊ฐ๋ ฅํ ๊ธฐ์ ์ค ํ๋๋ ๊ตฌ์กฐ ๋ถํด ํ ๋น(structural destructuring)์ ๋๋ค. ์ด๋ ํจํด ๋งค์นญ์ ํ ํํ๋ก, ๋ฐ์ดํฐ ๊ตฌ์กฐ(๊ฐ์ฒด ๋ฐ ๋ฐฐ์ด)์์ ์ฐ์ํ๊ณ ์ ํํ๊ฒ ๊ฐ์ ์ถ์ถํ ์ ์๊ฒ ํด์ค๋๋ค. ์ด ๊ธ์์๋ ๊ตฌ์กฐ ๋ถํด ํ ๋น์ ๋ณต์กํ ๋ด์ฉ๋ค์ ์ดํด๋ณด๊ณ , ์ฌ๋ฌ๋ถ์ ์๋ฐ์คํฌ๋ฆฝํธ ์ค๋ ฅ์ ํ ๋จ๊ณ ๋์ด์ฌ๋ฆด ์ ์๋ ์ค์ฉ์ ์ธ ์์ ์ ๋ชจ๋ฒ ์ฌ๋ก๋ฅผ ์ ๊ณตํฉ๋๋ค.
๊ตฌ์กฐ ๋ถํด ํ ๋น์ด๋ ๋ฌด์์ธ๊ฐ?
๊ตฌ์กฐ ๋ถํด ํ ๋น์ ES6(ECMAScript 2015) ๊ธฐ๋ฅ์ผ๋ก, ๊ฐ์ฒด์ ๋ฐฐ์ด์์ ๊ฐ์ ์ถ์ถํ์ฌ ๋ณ์์ ํ ๋นํ๋ ๊ฐ๊ฒฐํ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค. ์ ํ๊ธฐ๋ฒ(์: object.property)์ด๋ ๋ฐฐ์ด ์ธ๋ฑ์ค(์: array[0])๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ์ ์ ๊ทผํ๋ ๋์ , ๊ตฌ์กฐ ๋ถํด๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ์ผ์นํ๋ ํจํด์ ์ ์ํ์ฌ ํด๋นํ๋ ๋ณ์์ ๊ฐ์ ์๋์ผ๋ก ํ ๋นํ ์ ์์ต๋๋ค.
์ด๋ ์ฌ๋ฌ๋ถ์ด ์์ํ๋ ๋ฐ์ดํฐ์ "๋ชจ์"์ ์ ์ํ๋ฉด ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ์ถ์ถ์ ๋์ ์ฒ๋ฆฌํด์ฃผ๋ ์ ๊ตํ ํํ์ ํ ๋น์ด๋ผ๊ณ ์๊ฐํ ์ ์์ต๋๋ค. ์ด๋ ํนํ ๋ณต์กํ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ๋ค๋ฃฐ ๋ ์ฝ๋์ ๊ฐ๋ ์ฑ๊ณผ ์ ์ง๋ณด์์ฑ์ ๋์ฌ์ค๋๋ค.
๊ฐ์ฒด ๊ตฌ์กฐ ๋ถํด
๊ฐ์ฒด ๊ตฌ์กฐ ๋ถํด๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ์ฒด์์ ์์ฑ์ ์ถ์ถํ์ฌ ๊ฐ์ ์ด๋ฆ์ ๋ณ์(๋๋ ์ํ๋ ๊ฒฝ์ฐ ๋ค๋ฅธ ์ด๋ฆ์ ๋ณ์)์ ํ ๋นํ ์ ์์ต๋๋ค. ๊ธฐ๋ณธ ๊ตฌ๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
const { property1, property2 } = object;
์ค์ฉ์ ์ธ ์์ ๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค. ๊ธ๋ก๋ฒ ์ด์ปค๋จธ์ค ํ๋ซํผ์ ์ฌ์ฉ์๋ฅผ ๋ํ๋ด๋ user ๊ฐ์ฒด๊ฐ ์๋ค๊ณ ๊ฐ์ ํด ๋ด ์๋ค:
const user = {
id: 12345,
firstName: "Aisha",
lastName: "Khan",
country: "Pakistan",
email: "aisha.khan@example.com",
preferences: {
language: "Urdu",
currency: "PKR"
}
};
๊ธฐ๋ณธ์ ์ธ ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถํด
firstName๊ณผ lastName ์์ฑ์ ์ถ์ถํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ต๋๋ค:
const { firstName, lastName } = user;
console.log(firstName); // Output: Aisha
console.log(lastName); // Output: Khan
๊ตฌ์กฐ ๋ถํด ์ค ๋ณ์ ์ด๋ฆ ๋ณ๊ฒฝํ๊ธฐ
๋ค์ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ ์ถ์ถ๋ ๊ฐ์ ๋ค๋ฅธ ์ด๋ฆ์ ๋ณ์์ ํ ๋นํ ์๋ ์์ต๋๋ค:
const { firstName: givenName, lastName: familyName } = user;
console.log(givenName); // Output: Aisha
console.log(familyName); // Output: Khan
์ด๋ ์ด๋ฆ ์ถฉ๋์ ํผํ๊ณ ์ถ๊ฑฐ๋ ๋ ์ค๋ช ์ ์ธ ๋ณ์ ์ด๋ฆ์ ์ฌ์ฉํ๊ณ ์ถ์ ๋ ํนํ ์ ์ฉํฉ๋๋ค.
๊ธฐ๋ณธ๊ฐ ์ค์ ํ๊ธฐ
๋ง์ฝ ๊ฐ์ฒด์ ์์ฑ์ด ์กด์ฌํ์ง ์์ผ๋ฉด, ํด๋นํ๋ ๋ณ์์๋ undefined๊ฐ ํ ๋น๋ฉ๋๋ค. ์ด๋ฅผ ํผํ๊ธฐ ์ํด ๊ธฐ๋ณธ๊ฐ์ ์ ๊ณตํ ์ ์์ต๋๋ค:
const { age = 30 } = user;
console.log(age); // Output: 30 (since the user object doesn't have an 'age' property)
์ค์ฒฉ๋ ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถํด
์ค์ฒฉ๋ ๊ฐ์ฒด๋ ๊ตฌ์กฐ ๋ถํดํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, preferences ๊ฐ์ฒด์์ language์ currency๋ฅผ ์ถ์ถํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ํฉ๋๋ค:
const { preferences: { language, currency } } = user;
console.log(language); // Output: Urdu
console.log(currency); // Output: PKR
์ค์ฒฉ๋ ๊ตฌ์กฐ ๋ถํด ์ค์๋ ๋ณ์ ์ด๋ฆ์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค:
const { preferences: { language: preferredLanguage, currency: preferredCurrency } } = user;
console.log(preferredLanguage); // Output: Urdu
console.log(preferredCurrency); // Output: PKR
๊ธฐ๋ฅ ๊ฒฐํฉํ๊ธฐ
์ด๋ฆ ๋ณ๊ฒฝ, ๊ธฐ๋ณธ๊ฐ ์ค์ , ์ค์ฒฉ ๊ตฌ์กฐ ๋ถํด๋ฅผ ๊ฒฐํฉํ์ฌ ํจ์ฌ ๋ ์ ์ฐํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค:
const {
firstName: givenName,
lastName: familyName,
preferences: { language: preferredLanguage, currency: preferredCurrency = "USD" },
age = 30
} = user;
console.log(givenName); // Output: Aisha
console.log(familyName); // Output: Khan
console.log(preferredLanguage); // Output: Urdu
console.log(preferredCurrency); // Output: PKR
console.log(age); // Output: 30
๋๋จธ์ง ์์ฑ (Rest Properties)
๋๋ก๋ ํน์ ์์ฑ์ ์ถ์ถํ๊ณ ๋๋จธ์ง ์์ฑ๋ค์ ์๋ก์ด ๊ฐ์ฒด์ ๋ชจ์ผ๊ณ ์ถ์ ์ ์์ต๋๋ค. ์ด๋ ๋๋จธ์ง ์ฐ์ฐ์(...)๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌ์ฑํ ์ ์์ต๋๋ค:
const { id, firstName, lastName, ...remainingUserDetails } = user;
console.log(id); // Output: 12345
console.log(firstName); // Output: Aisha
console.log(lastName); // Output: Khan
console.log(remainingUserDetails); // Output: { country: "Pakistan", email: "aisha.khan@example.com", preferences: { language: "Urdu", currency: "PKR" } }
๋ฐฐ์ด ๊ตฌ์กฐ ๋ถํด
๋ฐฐ์ด ๊ตฌ์กฐ ๋ถํด๋ ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถํด์ ์ ์ฌํ์ง๋ง, ๋ฐฐ์ด์ ์ธ๋ฑ์ค ์์น๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ ์ถ์ถํฉ๋๋ค. ๊ธฐ๋ณธ ๊ตฌ๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
const [element1, element2] = array;
์ผ๋ณธ์ ์ธ๊ธฐ ๊ด๊ด์ง ๋ฐฐ์ด์ ์๋ก ๋ค์ด๋ณด๊ฒ ์ต๋๋ค:
const destinations = ["Tokyo", "Kyoto", "Osaka", "Hiroshima"];
๊ธฐ๋ณธ์ ์ธ ๋ฐฐ์ด ๊ตฌ์กฐ ๋ถํด
์ฒ์ ๋ ๋ชฉ์ ์ง๋ฅผ ์ถ์ถํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ต๋๋ค:
const [firstDestination, secondDestination] = destinations;
console.log(firstDestination); // Output: Tokyo
console.log(secondDestination); // Output: Kyoto
์์ ๊ฑด๋๋ฐ๊ธฐ
๊ตฌ์กฐ ๋ถํด ํจํด์์ ๋น ๊ณต๊ฐ์ ๋จ๊ฒจ ๋ฐฐ์ด์ ์์๋ฅผ ๊ฑด๋๋ธ ์ ์์ต๋๋ค:
const [,, thirdDestination] = destinations;
console.log(thirdDestination); // Output: Osaka
๊ธฐ๋ณธ๊ฐ ์ค์ ํ๊ธฐ
๊ฐ์ฒด ๊ตฌ์กฐ ๋ถํด์ ๋ง์ฐฌ๊ฐ์ง๋ก, ๋ฐฐ์ด ์์์ ๋ํ ๊ธฐ๋ณธ๊ฐ์ ์ ๊ณตํ ์ ์์ต๋๋ค:
const [first, second, third, fourth, fifth = "Nara"] = destinations;
console.log(fifth); // Output: Nara (since the array only has four elements)
๋๋จธ์ง ์์ (Rest Elements)
๋๋จธ์ง ์ฐ์ฐ์(...)๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์ ๋๋จธ์ง ์์๋ค์ ์๋ก์ด ๋ฐฐ์ด์ ๋ชจ์ ์ ์์ต๋๋ค:
const [firstDestination, ...otherDestinations] = destinations;
console.log(firstDestination); // Output: Tokyo
console.log(otherDestinations); // Output: ["Kyoto", "Osaka", "Hiroshima"]
์ค์ฒฉ๋ ๋ฐฐ์ด ๊ตฌ์กฐ ๋ถํด
์ค์ฒฉ๋ ๋ฐฐ์ด๋ ๊ตฌ์กฐ ๋ถํดํ ์ ์์ต๋๋ค:
const nestedArray = [1, [2, 3], 4];
const [one, [two, three], four] = nestedArray;
console.log(one); // Output: 1
console.log(two); // Output: 2
console.log(three); // Output: 3
console.log(four); // Output: 4
ํจ์ ๋งค๊ฐ๋ณ์์์์ ๊ตฌ์กฐ ๋ถํด
๊ตฌ์กฐ ๋ถํด๋ ํจ์ ๋งค๊ฐ๋ณ์๋ฅผ ๋ค๋ฃฐ ๋ ํนํ ์ ์ฉํฉ๋๋ค. ์ด๋ฅผ ํตํด ํจ์ ์๊ทธ๋์ฒ ๋ด์์ ์ง์ ์ธ์๋ก ์ ๋ฌ๋ ๊ฐ์ฒด๋ ๋ฐฐ์ด์์ ํน์ ์์ฑ์ ์ถ์ถํ ์ ์์ต๋๋ค.
ํจ์ ๋งค๊ฐ๋ณ์์์์ ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถํด
์ฌ์ฉ์ ์ ๋ณด๋ฅผ ํ์ํ๋ ํจ์๋ฅผ ์๊ฐํด ๋ด ์๋ค:
function displayUserInfo({ firstName, lastName, country }) {
console.log(`Name: ${firstName} ${lastName}, Country: ${country}`);
}
displayUserInfo(user); // Output: Name: Aisha Khan, Country: Pakistan
์ด๋ ํจ์ ๋ณธ๋ฌธ ๋ด์์ ์ง์ ์์ฑ์ ์ ๊ทผํ๋ ๊ฒ(์: user.firstName)๋ณด๋ค ํจ์ฌ ๊น๋ํ๊ณ ๊ฐ๋
์ฑ์ด ๋์ต๋๋ค.
ํจ์ ๋งค๊ฐ๋ณ์์์์ ๋ฐฐ์ด ๊ตฌ์กฐ ๋ถํด
๋ฐฐ์ด๋ก ์ฃผ์ด์ง ์ฌ๊ฐํ์ ์น์๋ฅผ ์ฌ์ฉํ์ฌ ๋ฉด์ ์ ๊ณ์ฐํ๋ ํจ์๊ฐ ์๋ค๊ณ ๊ฐ์ ํด ๋ด ์๋ค:
function calculateArea([width, height]) {
return width * height;
}
const dimensions = [10, 5];
const area = calculateArea(dimensions);
console.log(area); // Output: 50
๊ธฐ๋ณธ๊ฐ๊ณผ ๊ฒฐํฉํ๊ธฐ
ํจ์ ๋งค๊ฐ๋ณ์์์๋ ๊ตฌ์กฐ ๋ถํด์ ๊ธฐ๋ณธ๊ฐ์ ๊ฒฐํฉํ ์ ์์ต๋๋ค:
function greetUser({ name = "Guest", greeting = "Hello" }) {
console.log(`${greeting}, ${name}!`);
}
greetUser({ name: "Carlos" }); // Output: Hello, Carlos!
greetUser({}); // Output: Hello, Guest!
greetUser({ greeting: "Bonjour" }); // Output: Bonjour, Guest!
์ค์ฉ์ ์ธ ์ฌ์ฉ ์ฌ๋ก ๋ฐ ์์
๊ตฌ์กฐ ๋ถํด๋ ๋ค์ํ ์๋๋ฆฌ์ค์ ์ ์ฉํ ์ ์์ต๋๋ค. ๋ค์์ ๋ช ๊ฐ์ง ์ค์ฉ์ ์ธ ์์ ์ ๋๋ค:
1. API ์๋ต ์ฒ๋ฆฌ
API์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ๋, ์ข ์ข ๋ณต์กํ ๊ตฌ์กฐ์ JSON ์๋ต์ ๋ฐ์ต๋๋ค. ๊ตฌ์กฐ ๋ถํด๋ ํ์ํ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ๋ ๊ณผ์ ์ ๋จ์ํํ ์ ์์ต๋๋ค.
async function fetchWeatherData(city) {
const response = await fetch(`https://api.example.com/weather?q=${city}`);
const data = await response.json();
// Destructure the relevant data
const { main: { temp, humidity }, weather: [{ description }] } = data;
console.log(`Temperature: ${temp}ยฐC, Humidity: ${humidity}%, Description: ${description}`);
}
fetchWeatherData("London");
2. ๋ฆฌ์กํธ ์ปดํฌ๋ํธ
๋ฆฌ์กํธ์์๋ ์ปดํฌ๋ํธ์ ์ ๋ฌ๋ props๋ฅผ ์ถ์ถํ๋ ๋ฐ ๊ตฌ์กฐ ๋ถํด๊ฐ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋ฉ๋๋ค:
function UserProfile({ firstName, lastName, email }) {
return (
<div>
<h2>{firstName} {lastName}</h2>
<p>Email: {email}</p>
</div>
);
}
3. ๋ฆฌ๋์ค ๋ฆฌ๋์
๊ตฌ์กฐ ๋ถํด๋ ๋ฆฌ๋์ค ๋ฆฌ๋์์์ ์ก์ ๊ณผ ์ํ๋ฅผ ๋ค๋ฃจ๋ ์์ ์ ๋จ์ํํฉ๋๋ค:
function cartReducer(state = initialState, action) {
switch (action.type) {
case "ADD_TO_CART":
const { productId, quantity } = action.payload;
// ...
return { ...state, /* ... */ };
default:
return state;
}
}
4. ์ค์ ๊ฐ์ฒด
์ค์ ๊ฐ์ฒด๋ฅผ ๋ค๋ฃฐ ๋, ๊ตฌ์กฐ ๋ถํด๋ ํน์ ์ค์ ์ ์ฝ๊ฒ ์ถ์ถํ๊ณ ์ฌ์ฉํ ์ ์๊ฒ ํด์ค๋๋ค:
const config = {
apiKey: "YOUR_API_KEY",
apiUrl: "https://api.example.com",
timeout: 5000,
retries: 3
};
const { apiKey, apiUrl, timeout } = config;
console.log(`Using API key: ${apiKey}, API URL: ${apiUrl}, Timeout: ${timeout}`);
5. ๋ณ์ ๊ฐ ๊ตํํ๊ธฐ
๊ตฌ์กฐ ๋ถํด๋ ์์ ๋ณ์๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๋ ๋ณ์์ ๊ฐ์ ๊ตํํ๋ ๊ฐ๊ฒฐํ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค:
let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a); // Output: 2
console.log(b); // Output: 1
๋ชจ๋ฒ ์ฌ๋ก ๋ฐ ๊ณ ๋ ค ์ฌํญ
- ๊ฐ๋ ์ฑ: ์ฝ๋๋ฅผ ๋ ์ฝ๊ธฐ ์ฝ๊ณ ์์ฒด์ ์ผ๋ก ์ค๋ช ์ด ๋๋๋ก ๊ตฌ์กฐ ๋ถํด๋ฅผ ์ฌ์ฉํ์ธ์.
- ์ ์ง๋ณด์์ฑ: ๊ตฌ์กฐ ๋ถํด๋ ์ฝ๋ ์ค๋ณต์ ์ค์ด๊ณ ์ฝ๋ ์ ์ง๋ณด์๋ฅผ ๋ ์ฝ๊ฒ ๋ง๋ญ๋๋ค.
- ๋ณต์ก์ฑ: ๊ณผ๋ํ ๊ตฌ์กฐ ๋ถํด, ํนํ ๊น๊ฒ ์ค์ฒฉ๋ ๊ฐ์ฒด์ ๋ํ ๊ตฌ์กฐ ๋ถํด๋ ์ฝ๋๋ฅผ ์ดํดํ๊ธฐ ์ด๋ ต๊ฒ ๋ง๋ค ์ ์์ผ๋ฏ๋ก ํผํ์ธ์.
- ๊ธฐ๋ณธ๊ฐ: ์๊ธฐ์น ์์
undefined๊ฐ์ ํผํ๊ธฐ ์ํด ํญ์ ๊ธฐ๋ณธ๊ฐ์ ์ ๊ณตํ๋ ๊ฒ์ ๊ณ ๋ คํ์ธ์. - ์ค๋ฅ ์ฒ๋ฆฌ: ๊ตฌ์กฐ ๋ถํด๋ฅผ ํ ๋, ํนํ API์ ๊ฐ์ ์ธ๋ถ ๋ฐ์ดํฐ ์์ค๋ฅผ ๋ค๋ฃฐ ๋ ๋ฐ์ํ ์ ์๋ ์ ์ฌ์ ์ค๋ฅ์ ์ ์ํ์ธ์. ๋๋ฝ๋๊ฑฐ๋ ์ ํจํ์ง ์์ ๋ฐ์ดํฐ๋ฅผ ์ ์์ ์ผ๋ก ์ฒ๋ฆฌํ๊ธฐ ์ํ ์ค๋ฅ ์ฒ๋ฆฌ ๋ฉ์ปค๋์ฆ์ ์ถ๊ฐํ๋ ๊ฒ์ ๊ณ ๋ คํ์ธ์.
- ์ฝ๋ ์คํ์ผ: ์ฝ๋๋ฒ ์ด์ค ์ ์ฒด์์ ๊ตฌ์กฐ ๋ถํด๊ฐ ์ผ๊ด๋๊ฒ ์ฌ์ฉ๋๋๋ก ์ผ๊ด๋ ์ฝ๋ฉ ์คํ์ผ ๊ฐ์ด๋๋ผ์ธ์ ๋ฐ๋ฅด์ธ์.
๊ณ ๊ธ ๊ธฐ๋ฒ
๋์ ์์ฑ ์ด๋ฆ
๊ตฌ์กฐ ๋ถํด๋ ๋ณดํต ์๋ ค์ง ์์ฑ ์ด๋ฆ์ ํฌํจํ์ง๋ง, ๊ณ์ฐ๋ ์์ฑ ์ด๋ฆ(ES6์ ๋์ )์ ์ฌ์ฉํ์ฌ ๋์ ํค๋ฅผ ๊ฐ์ง ์์ฑ์ ๊ตฌ์กฐ ๋ถํดํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ด๋ ๋ ์ผ๋ฐ์ ์ด๋ฉฐ ์ ์คํ ๊ณ ๋ ค๊ฐ ํ์ํฉ๋๋ค.
const key = 'dynamicProperty';
const obj = { [key]: 'Value' };
// Note: Cannot directly destructure with dynamic keys like this
// const { [key]: value } = obj; // This doesn't work as expected
// Instead, you'd typically access it directly or use an intermediate variable
const value = obj[key];
console.log(value); // Output: Value
์ง์ ์ ์ธ ๊ตฌ์กฐ ๋ถํด ๊ธฐ๋ฅ์ ์๋์ง๋ง, ๊ณ์ฐ๋ ์์ฑ ์ด๋ฆ์ ๊ตฌ์กฐ ๋ถํด ์์ ์ ํค๊ฐ ์๋ ค์ ธ ์์ง๋ง ๋ณ์์ ์ ์ฅ๋์ด ์๋ ๊ฒฝ์ฐ, ๋ ๋์ ์ธ ๋ฐ์ดํฐ ์กฐ์์ ์ํด ์ผ๋ถ ์๋๋ฆฌ์ค์์ ๊ตฌ์กฐ ๋ถํด์ *ํจ๊ป* ์ฌ์ฉ๋ ์ ์์ต๋๋ค.
๊ฐ์ฒด๋ ๋ฐฐ์ด์ ๋ฐํํ๋ ํจ์์ ํจ๊ป ๊ตฌ์กฐ ๋ถํดํ๊ธฐ
ํจ์๊ฐ ๊ฐ์ฒด๋ ๋ฐฐ์ด์ ๋ฐํํ๋ ๊ฒฝ์ฐ, ํจ์ ํธ์ถ์ ๊ฒฐ๊ณผ๋ฅผ ์ง์ ๊ตฌ์กฐ ๋ถํดํ ์ ์์ต๋๋ค. ์ด๋ ๋ณต์กํ ์์ ์์ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ๋ ๋ฐ ์ ์ฉํ ์ ์์ต๋๋ค.
function createPoint() {
return { x: 10, y: 20 };
}
const { x, y } = createPoint();
console.log(x, y); // Output: 10 20
function getCoordinates() {
return [30, 40];
}
const [latitude, longitude] = getCoordinates();
console.log(latitude, longitude); // Output: 30 40
๊ฒฐ๋ก
๊ตฌ์กฐ ๋ถํด ํ ๋น์ ์๋ฐ์คํฌ๋ฆฝํธ์์ ์ฝ๋์ ๊ฐ๋ ์ฑ, ์ ์ง๋ณด์์ฑ, ๊ฐ๊ฒฐ์ฑ์ ํฅ์์ํค๋ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ์ ๋๋ค. ๊ฐ์ฒด ๋ฐ ๋ฐฐ์ด ๊ตฌ์กฐ ๋ถํด๋ฅผ ๋ง์คํฐํจ์ผ๋ก์จ, ํนํ ๋ณต์กํ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ๋ค๋ฃฐ ๋ ๋ ์ฐ์ํ๊ณ ํจ์จ์ ์ธ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ต๋๋ค. ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก์ ํธ์์ ๊ตฌ์กฐ ๋ถํด๋ฅผ ์ ๊ทน์ ์ผ๋ก ํ์ฉํ์ฌ ๊ทธ ์ ์ฌ๋ ฅ์ ์ต๋ํ ๋ฐํํ๊ณ ํ๋ก๊ทธ๋๋ฐ ๊ธฐ์ ์ ํฅ์์ํค์ธ์. ์ฝ๋๊ฐ ์ดํดํ๊ณ ๋๋ฒ๊น ํ๊ธฐ ์ฝ๋๋ก ๊ตฌ์กฐ ๋ถํด์ ๊ฐ๋ ฅํจ๊ณผ ์ฝ๋์ ๋ช ํ์ฑ ๋ฐ ์ ์ง๋ณด์์ฑ ์ฌ์ด์ ๊ท ํ์ ๋ง์ถ๋ ๊ฒ์ ์์ง ๋ง์ธ์.
์์ ํ๋ฆ์ ๊ตฌ์กฐ ๋ถํด ํ ๋น์ ํตํฉํจ์ผ๋ก์จ ์ฝ๋ ํ์ง์ ํฅ์์ํฌ ๋ฟ๋ง ์๋๋ผ ์๋ฐ์คํฌ๋ฆฝํธ์ ๊ธฐ๋ฅ์ ๋ํ ๋ ๊น์ ์ดํด๋ฅผ ์ป๊ฒ ๋ ๊ฒ์ ๋๋ค. ์ด๋ ๊ฒฐ๊ณผ์ ์ผ๋ก ์ค๋๋ ์ ์ญ๋์ ์ธ ๊ธฐ์ ํ๊ฒฝ์์ ์ฌ๋ฌ๋ถ์ ๋ ๋ฅ์ํ๊ณ ๊ฐ์น ์๋ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ๋ฐ์๋ก ๋ง๋ค์ด ์ค ๊ฒ์ ๋๋ค.